package com.general.client.widgets.styledlist;

import com.general.client.basewidget.BaseWidget;
import com.general.client.bundle.CommonBundle;
import com.general.client.widgets.taperedbox.TaperedBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class StyledList extends BaseWidget {

	@UiField
	TaperedBox topContainer;

	@UiField
	FlowPanel widgetsContainer;

	@UiField
	CommonBundle bundle;

	private int minHeight = 0;

	private int maxHeight = 9999;

	private boolean showScrollY = true;

	private static StyledListUiBinder uiBinder = GWT
			.create(StyledListUiBinder.class);

	interface StyledListUiBinder extends UiBinder<Widget, StyledList> {
	}

	public StyledList() {
		initWidget(uiBinder.createAndBindUi(this));
		setStyles();
	}

	public void add(Widget w) {
		FlowPanel fp = new FlowPanel();
		fp.add(w);
		fp.addStyleName(bundle.style().StyledListItem());
		widgetsContainer.add(fp);

	}

	public void setMinHeight(int minHeight) {
		this.minHeight = minHeight;
		setStyles();
	}

	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
		setStyles();
	}

	public void setShowScrollY(boolean showScrollY) {
		this.showScrollY = showScrollY;
		setStyles();
	}

	private void setStyles() {
		String scrollY = " overflow-y: hidden";
		if (showScrollY) {
			scrollY = " overflow-y: scroll;";
		}

		widgetsContainer.getElement().setAttribute(
				"style",
				"min-height: " + minHeight + "px; max-height: " + maxHeight
						+ "px;" + scrollY + " overflow-x:hidden;");

	}
}
